﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ForumLibrary.Data;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            string username = Session["username"].ToString();
            ForumManager manager = new ForumWebManager();
            manager.logout(username);
            Session.Abandon();
        }
        //Session.Remove("username");
        Label welcomeLabel = (Label)Master.FindControl("WelcomeLabel");
        welcomeLabel.Text = "";
        welcomeLabel.Visible = false;

        HyperLink logoutLink = (HyperLink)Master.FindControl("LogoutLink");
        logoutLink.Visible = false;
    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(UsernameTextBox.Text) || string.IsNullOrEmpty(PasswordTextBox.Text))
        {
            FieldProblemLabel.Text = "Username or Password incorrect";
            FieldProblemLabel.Visible = true;
        }
        else
        {
        	string loginValue = ObjectDataSource1.Select().ToString();
            if (loginValue.Contains("Welcome Moderator"))
            {
                Session.Add("isModerator", true);
                Session.Add("username", UsernameTextBox.Text);
                Response.Redirect("~/Forums.aspx");
            }
            else if (loginValue.Contains("Welcome"))
            {
                Session.Add("isModerator", false);
                Session.Add("username", UsernameTextBox.Text);
                Response.Redirect("~/Forums.aspx");
            }
            else
            {
                FieldProblemLabel.Text = loginValue;
                FieldProblemLabel.Visible = true;
            }
        }
    }
    protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        
    }
}